//
//  AppDelegate.swift
//  CooperSwift
//
//  Created by CooperSwiftappkf@163.com on 04/26/2021.
//  Copyright (c) 2021 CooperSwiftappkf@163.com. All rights reserved.
//

import UIKit

import IQKeyboardManager
import Bugly
import CooperSwift
import Kingfisher

import Toast_Swift

//import Flutter
//import FlutterPluginRegistrant

//@main
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var window: UIWindow?

//@UIApplicationMain
//class AppDelegate: FlutterAppDelegate {
    
    var mainVC: UIViewController!
    var navVC: NavigationC!
    
//    lazy var flutterEngine = FlutterEngine(name: "my flutter engine")

    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    
//        flutterEngine.run()
//        GeneratedPluginRegistrant.register(with: self.flutterEngine);
        
        AppConfig.awake()
        
        configOther(launchOptions: launchOptions)
        
        configRootVC()
        
        return true
    }

    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}


//MARK: -- 启动配置
extension AppDelegate {
    
    @objc private func configRootVC() {
        
        DispatchQueue.once {
            NotificationCenter.default.addObserver(self, selector: #selector(self.configRootVC), name: NSNotification.Name(rawValue: Constants.LoginChangedNotifyKey), object: nil)
        }
    
        let tabC = TabbarC(tabModels:
                            [
                                TabModel(title: "零", image: UIImage(named: "tabbar0-normal"), selecedIMG: UIImage(named: "tabbar0-selected")),
                                TabModel(title: "一", image: UIImage(named: "tabbar0-normal"), selecedIMG: UIImage(named: "tabbar0-selected")),
                                TabModel(title: "二", image: UIImage(named: "tabbar1-normal"), selecedIMG: UIImage(named: "tabbar1-selected")),
                                TabModel(title: "三", image: UIImage(named: "tabbar2-normal"), selecedIMG: UIImage(named: "tabbar2-selected"))
                            ],
                           childVCs:
                            [
                                NavigationC(rootViewController:  UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController")),
                                NavigationC(rootViewController:  TestPageContentVC()),
                                NavigationC(rootViewController:  TestPageContent2VC()),
                                NavigationC(rootViewController:  TestPageContent3VC())
                            ]
                        )
        
        self.navVC = NavigationC(rootViewController: tabC)
        
        self.mainVC = tabC
        
        self.window?.backgroundColor = .white
        self.window?.rootViewController = self.navVC
        self.window?.makeKeyAndVisible()
    }
    
    @objc private func configOther(launchOptions: [UIApplication.LaunchOptionsKey: Any]?) {
        // 键盘配置
        IQKeyboardManager.shared().isEnabled = true
        IQKeyboardManager.shared().toolbarDoneBarButtonItemText = "确定"
        Bugly.start(withAppId: "63dbb3d927")
        // 推送
        JPushHandler.shared.configJpush(launchOptions: launchOptions)
        // 本地推送
        NotificationHandler.shared.configPush()
        // 高德地图
        AMapHandler.shared.configAMap()
        // 网络状况
        ReachabilityHandler.shared.startNotifier()
        // kingfisher
        setupKingfisher()
        
    }
    
    // MARK: - Kingfisher
    func setupKingfisher() {
        // Set maximum disk cache size for default cache. Default value is 0, which means no limit.
        ImageCache.default.diskStorage.config.sizeLimit = UInt(500 * 1024 * 1024) // 500 MB
        //cache.memoryStorage.config.totalCostLimit = 300 * 1024 * 1024
        // Set longest time duration of the cache being stored in disk. Default value is 1 week
        ImageCache.default.diskStorage.config.expiration = .days(7)
        // Set timeout duration for default image downloader. Default value is 15 sec.
        ImageDownloader.default.downloadTimeout = 10.0
    }
    
}

